package com.rankeiot.core.util.castor;

import java.util.HashSet;
import java.util.Set;

@SuppressWarnings("AlibabaConstantFieldShouldBeUpperCase")
public class BooleanCastor implements Castor<Boolean> {
    public static final BooleanCastor instance=new BooleanCastor();
    private Set<String> falseSet=new HashSet<String>();
    private BooleanCastor(){
        falseSet.add("");
        falseSet.add("0");
        falseSet.add("-1");
        falseSet.add("n");
        falseSet.add("f");
        falseSet.add("null");
        falseSet.add("false");
    }
    @Override
    public Boolean cast(Object obj) {
        if (obj == null) {
            return false;
        } else if (obj instanceof Number) {
            return ((Number) obj).doubleValue() > 0.0D;
        } else if (obj instanceof Boolean) {
            return (Boolean)obj;
        } else if (obj instanceof String) {
            if(falseSet.contains(((String) obj).toLowerCase())){
                return false;
            }else{
                try {
                    return Double.parseDouble((String) obj) > 0.0D;
                }catch (NumberFormatException e){
                    return true;
                }
            }
        }
        return false;
    }
}
